package com.yanglei.demo.client;


import com.yanglei.demo.client.dto.CommonResult;
import com.yanglei.demo.client.dto.user.OAuth2UserInfoRespDTO;
import com.yanglei.demo.client.dto.user.UserUpdateReqDTO;
import com.yanglei.demo.framework.LoginUser;
import com.yanglei.demo.framework.core.util.SecurityUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @ClassName UserClient
 * @Description TODO
 * @Author hljstart
 * @Date 2025/11/4 20:39
 * @Version 1.0
 */
@Component
public class UserClient {

    @Resource
    private RestTemplate restTemplate;

    private static final String BASE_URL = "http://127.0.0.1:48080/admin-api/system/oauth2/user";

    /**
     * 租户编号，默认使用1
     */
    private static final Long TENANT_ID = 1L;

    private static final String CLIENT_ID = "yudao-sso-demo-by-code1";

    private static final String CLIENT_SECRET = "test";

    public CommonResult<OAuth2UserInfoRespDTO> getUserInfo() {
        // 1、构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("tenant-id", TENANT_ID.toString());
        // 设置请求token
        addTokenHeader(headers);
        // 没有请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        ResponseEntity<CommonResult<OAuth2UserInfoRespDTO>> exchange = restTemplate.exchange(
                BASE_URL + "/get",
                HttpMethod.GET,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<CommonResult<OAuth2UserInfoRespDTO>>() {
                }
        );
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "请求必须成功 200");

        return exchange.getBody();
    }

    private static void addTokenHeader(HttpHeaders headers) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Assert.notNull(loginUser, "登录用户不能为空");
        headers.add("Authorization", "Bearer " + loginUser.getAccessToken());
    }

    public CommonResult<Boolean> update(UserUpdateReqDTO updateReqDTO) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("tenant-id", TENANT_ID.toString());
        addTokenHeader(headers);
        ResponseEntity<CommonResult<Boolean>> exchange = restTemplate.exchange(
                BASE_URL + "/update",
                HttpMethod.PUT,
                new HttpEntity<>(updateReqDTO, headers),
                new ParameterizedTypeReference<CommonResult<Boolean>>() {
                }

        );
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "请求必须成功 200");
        return exchange.getBody();
    }
}
